package com.aplink.generic.xmlparser;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import android.content.res.Resources;
import android.text.TextUtils;

import com.aplink.generic.pojo.MainMenu;
import com.aplink.generic.pojo.MainMenuItem;
import com.aplink.generic.util.ResourcesHelper;

public class MainMenuParser {
    public static String ACTIVITY_NAME = "activityName";
    public static String ICON = "icon";
    public static String ID = "id";
    public static String MAIN_MENU = "mainMenu";
    public static String MENU = "menu";
    public static String TITLE = "title";

    public static MainMenu parse(final int xmlFileID) {
        return MainMenuParser.parserIntoControls(xmlFileID,
                MainMenuParser.MAIN_MENU);
    }

    public static MainMenu parse(Node node) {
        final MainMenu mainMenu = new MainMenu();
        final Element element = (Element) node;
        final NodeList nodeList = element
                .getElementsByTagName(MainMenuParser.MENU);
        for (int i = 0; i < nodeList.getLength(); i++) {
            node = nodeList.item(i);
            if (node.hasAttributes()) {
                final MainMenuItem mainMenuItem = new MainMenuItem();
                String value = XMLPaserUtil.getAttrValue(node,
                        MainMenuParser.ICON);
                if (!TextUtils.isEmpty(value)) {
                    mainMenuItem.setIcon(value);
                }

                value = XMLPaserUtil.getAttrValue(node,
                        MainMenuParser.ACTIVITY_NAME);
                if (!TextUtils.isEmpty(value)) {
                    mainMenuItem.setActivityName(value);
                }

                value = XMLPaserUtil.getAttrValue(node, MainMenuParser.TITLE);
                if (!TextUtils.isEmpty(value)) {
                    mainMenuItem.setTitle(ResourcesHelper
                            .getStringByName(value));
                }
                value = XMLPaserUtil.getAttrValue(node, MainMenuParser.ID);
                if (!TextUtils.isEmpty(value)) {
                    mainMenuItem.setID(value);
                }
                mainMenu.put(mainMenuItem.getID(), mainMenuItem);
            }
        }
        return mainMenu;
    }

    private static MainMenu parserIntoControls(final int xmlfileId,
            final String tagname) {
        try {
            final Document doc = XMLPaserUtil.openDocumentFrom(xmlfileId);
            return MainMenuParser.parseXML(doc, tagname);
        } catch (final ParserConfigurationException e) {
            e.printStackTrace();
        } catch (final SAXException e) {
            e.printStackTrace();
        } catch (final IOException e) {
            e.printStackTrace();
        } catch (final Resources.NotFoundException e) {
            e.printStackTrace();
        }
        return new MainMenu();
    }

    private static MainMenu parseXML(final Document doc, final String tagName) {
        final NodeList nList = doc.getElementsByTagName(tagName);
        if (nList.getLength() == 1) {
            final Node nNode = nList.item(0);
            return MainMenuParser.parse(nNode);
        }
        return null;
    }
}
